package cn.iocoder.yudao.module.wms.controller.admin.person;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.wms.controller.admin.person.vo.PersonPageReqVO;
import cn.iocoder.yudao.module.wms.controller.admin.person.vo.PersonRespVO;
import cn.iocoder.yudao.module.wms.controller.admin.person.vo.PersonSaveReqVO;
import cn.iocoder.yudao.module.wms.dal.dataobject.person.PersonDO;
import cn.iocoder.yudao.module.wms.service.person.PersonService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;

@Tag(name = "管理后台 - 业务员档案")
@RestController
@RequestMapping("/wms/person")
@Validated
public class PersonController {

    @Resource
    private PersonService personService;

    @PostMapping("/create")
    @Operation(summary = "创建业务员档案")
    @PreAuthorize("@ss.hasPermission('wms:person:create')")
    public CommonResult<Long> createPerson(@Valid @RequestBody PersonSaveReqVO createReqVO) {
        return success(personService.createPerson(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新业务员档案")
    @PreAuthorize("@ss.hasPermission('wms:person:update')")
    public CommonResult<Boolean> updatePerson(@Valid @RequestBody PersonSaveReqVO updateReqVO) {
        personService.updatePerson(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除业务员档案")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wms:person:delete')")
    public CommonResult<Boolean> deletePerson(@RequestParam("id") Long id) {
        personService.deletePerson(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得业务员档案")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wms:person:query')")
    public CommonResult<PersonRespVO> getPerson(@RequestParam("id") Long id) {
        PersonDO person = personService.getPerson(id);
        return success(BeanUtils.toBean(person, PersonRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得业务员档案分页")
    @PreAuthorize("@ss.hasPermission('wms:person:query')")
    public CommonResult<PageResult<PersonRespVO>> getPersonPage(@Valid PersonPageReqVO pageReqVO) {
        PageResult<PersonDO> pageResult = personService.getPersonPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PersonRespVO.class));
    }

    @GetMapping("/simple-list")
    @Operation(summary = "获得业务员精简列表", description = "主要用于前端的下拉选项")
    @PreAuthorize("@ss.hasPermission('wms:person:query')")
    public CommonResult<List<PersonRespVO>> getPersonSimpleList() {
        List<PersonDO> list = personService.getPersonList();
        return success(convertList(list, person -> new PersonRespVO()
                .setId(person.getId()).setName(person.getName()).setDeptId(person.getDeptId())));
    }

    @GetMapping("/getPersonList")
    @Operation(summary = "获得业务员精简列表", description = "主要用于前端的下拉选项")
    @PreAuthorize("@ss.hasPermission('wms:person:query')")
    public CommonResult<List<PersonDO>> getPersonList(@Valid PersonDO personDO) {
        List<PersonDO> list = personService.getPersonList(personDO);
        return success(list);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出业务员档案 Excel")
    @PreAuthorize("@ss.hasPermission('wms:person:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPersonExcel(@Valid PersonPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PersonDO> list = personService.getPersonPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "业务员档案.xls", "数据", PersonRespVO.class,
                        BeanUtils.toBean(list, PersonRespVO.class));
    }

}